package com.yy.wga.income.service.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yy.wga.common.core.dto.Result;
import com.yy.wga.common.util.common.StringUtils;
import com.yy.wga.common.web.intereceptor.RequestHolder;
import com.yy.wga.income.feign.api.IncomeUserBalanceFeign;
import com.yy.wga.income.feign.request.IncomeUserBalanceListRequest;
import com.yy.wga.income.feign.request.IncomeUserBalanceWithdrawRequest;
import com.yy.wga.income.feign.response.IncomeUserBalanceGetInfoResponse;
import com.yy.wga.income.feign.response.IncomeUserBalanceInfoResponse;
import com.yy.wga.income.service.pojo.IncomeOrderGoodsDO;
import com.yy.wga.income.service.pojo.IncomeUserBalanceDO;
import com.yy.wga.income.service.pojo.IncomeWithdrawSettingDO;
import com.yy.wga.income.service.service.IncomeOrderGoodsService;
import com.yy.wga.income.service.service.IncomeUserBalanceService;
import com.yy.wga.income.service.service.IncomeWithdrawSettingService;
import lombok.RequiredArgsConstructor;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * 用户余额
 *
 * @author guochao
 * @since 2024-11-12 00:27:47
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("api/income/userBalance")
public class IncomeUserBalanceController implements IncomeUserBalanceFeign {
    /**
     * 服务对象
     */
    private final IncomeUserBalanceService incomeUserBalanceService;
    private final IncomeOrderGoodsService incomeOrderGoodsService;
    private final IncomeWithdrawSettingService withdrawSettingService;

    @Override
    public Result<IncomeUserBalanceGetInfoResponse> getUserBalance(@RequestParam("userId") Long userId) {
        IncomeUserBalanceDO userBalanceDO = incomeUserBalanceService.getOne(new LambdaQueryWrapper<IncomeUserBalanceDO>()
                .eq(IncomeUserBalanceDO::getAppKey, RequestHolder.getBaseRequest().getAppKey())
                .in(IncomeUserBalanceDO::getUserId, userId));
        if (userBalanceDO == null) {
            return Result.success(new IncomeUserBalanceGetInfoResponse());
        }
        IncomeUserBalanceGetInfoResponse response = new IncomeUserBalanceGetInfoResponse();
        response.setSettle(userBalanceDO.getSettle());
        response.setTotal(userBalanceDO.getSettle() + userBalanceDO.getInWithdraw() + userBalanceDO.getWithdraw());
        response.setUnSettle(userBalanceDO.getUnSettle());

        // 今日结算
        List<IncomeOrderGoodsDO> incomeOrderGoodsList = incomeOrderGoodsService.list(new LambdaQueryWrapper<IncomeOrderGoodsDO>()
                .eq(IncomeOrderGoodsDO::getAppKey, RequestHolder.getBaseRequest().getAppKey())
                .eq(IncomeOrderGoodsDO::getUserId, userId)
                .ge(IncomeOrderGoodsDO::getSettleTime, new Date()));
        if (!CollectionUtils.isEmpty(incomeOrderGoodsList)) {
            int sum = incomeOrderGoodsList.stream()
                    .mapToInt(IncomeOrderGoodsDO::getCommissionValue)
                    .sum();
            response.setTodaySettle(sum);
        }
        IncomeWithdrawSettingDO withdrawSetting = withdrawSettingService.getOne(new LambdaQueryWrapper<IncomeWithdrawSettingDO>()
                .eq(IncomeWithdrawSettingDO::getAppKey, RequestHolder.getBaseRequest().getAppKey()));
        com.yy.wga.common.utils.mapping.BeansUtils.copyProperties(withdrawSetting, response);
        return Result.success(response);
    }

    @Override
    public Result<List<IncomeUserBalanceInfoResponse>> listByUserId(@RequestBody @Valid IncomeUserBalanceListRequest request) {
        List<IncomeUserBalanceDO> userBalanceList = incomeUserBalanceService.list(new LambdaQueryWrapper<IncomeUserBalanceDO>()
                .eq(IncomeUserBalanceDO::getAppKey, RequestHolder.getBaseRequest().getAppKey())
                .in(IncomeUserBalanceDO::getUserId, request.getUserIds()));
        if (CollectionUtils.isEmpty(userBalanceList)) {
            return Result.success(Collections.emptyList());
        }
        return Result.success(com.yy.wga.common.utils.mapping.BeansUtils.mapList(userBalanceList, IncomeUserBalanceInfoResponse.class));
    }

    @Override
    public Result<Boolean> withdraw(@RequestBody @Valid IncomeUserBalanceWithdrawRequest request) {
        if (request.getType().equals(2)) {
            if (StringUtils.isBlank(request.getWithdrawAccount()) || StringUtils.isBlank(request.getRealName())) {
                return Result.fail("请输入提现账号");
            }
        }
        incomeUserBalanceService.withdraw(request);
        return Result.success(Boolean.TRUE);
    }
}
